package com.gator.rabbitmq.pablo_rabbitmq.direct;


import com.gator.rabbitmq.pablo_rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @Author PABLO
 * @Date 2022/6/5 13:37
 * @Desc 通过交换机-路由key发送到指定队列
 */
public class Direct_Provider {


    public static final String EXCHANGE_NAME = "pablo-exchange-direct";
    public static final String EXCHANGE_TYPE = "direct";

    public static void main(String[] args) {
        sendMsg();
    }

    private static void sendMsg() {
        Channel channel = ConnectionUtils.getConnectionAndChannel();
        try {

            //声明交换机,类型为扇出广播
            channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()) {
                String msg = scanner.next();
                channel.basicPublish(EXCHANGE_NAME, "direct2", null, msg.getBytes(StandardCharsets.UTF_8));
                System.out.println("生产者发出消息" + msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
